#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<int> k[25];
int n;
typedef pair<int,int> PII;

PII find(int x){
	for(int i = 0;i < n;i++){
		for(int j = 0;j < k[i].size();j++){
			if(k[i][j] == x){
				return {i,j};
			}
		}
	}
}

void turnba(int x,int y){
	for(int i = y + 1;i < k[x].size();i++){
		k[k[x][i]].push_back(k[x][i]); 
	}
	k[x].resize(y+1);
}

int main(){
	cin >> n;

	for(int i = 0;i < n;i++){
		k[i].push_back(i);
	}
	string str;
	while(cin >> str){
		int a,b,p;
		if(str == "move"){
			cin >> a;
			cin >> str;
			cin >> b;
			if(a==b)continue;
			PII poa=find(a);
			int xa = poa.first,ya = poa.second;
			PII pob=find(b);
			int xb = pob.first,yb = pob.second;
			if(str == "onto"){
				turnba(xa,ya);
				turnba(xb,yb);
				k[xa].resize(ya);
				k[xb].push_back(a);
			}
			else if(str == "over"){
				turnba(xa,ya);
				k[xa].resize(ya);
				k[xb].push_back(a);
			}
			}
		else if(str == "quit") break;
		else if(str == "pile"){
			cin >> a;
			cin >> str;
			cin >> b;
			if(a==b)continue;
			PII poa=find(a);
			int xa = poa.first,ya = poa.second;
			PII pob=find(b);
			int xb = pob.first,yb = pob.second;
			if(xa==xb) continue;
			if(str == "onto"){
				turnba(xb,yb);
				for(int i = ya;i < k[xa].size();i++){
					k[xb].push_back(k[xa][i]);
				}
				k[xa].resize(ya);
			}
			else if(str == "over"){
				for(int i = ya;i < k[xa].size();i++){
					k[xb].push_back(k[xa][i]);
				}
				k[xa].resize(ya);
			}
		}
		}
		for(int i = 0;i < n;i++){
			cout << i << ": " ;
			for(int j = 0;j < k[i].size();j++){
				cout << k[i][j] << " ";
			}
			cout << endl;
		}
	return 0;
	}
